<template>
  <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
        <el-form-item label="生产单号" prop="produceNo">
          <el-input v-model="queryParams.produceNo" placeholder="请输入生产单号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="工程名称" prop="gongchengName">
          <el-input v-model="queryParams.gongchengName" placeholder="请输入工程名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="产品编码" prop="goodsCode">
          <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="产品名称" prop="goodsName">
          <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="产品型号" prop="goodModel">
          <el-input v-model="queryParams.goodModel" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="工序名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入工序名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="图纸号" prop="shuoming">
          <el-input v-model="queryParams.shuoming" placeholder="请输入图纸号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="生产产品名称" prop="gongxuName">
          <el-input v-model="queryParams.gongxuName" placeholder="请输入生产产品名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="生产产品型号" prop="gongxuModel">
          <el-input v-model="queryParams.gongxuModel" placeholder="请输入生产产品型号" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="车间" prop="deptName">
          <el-select v-model="queryParams.deptName" placeholder="请选择车间" style="width: 100%" @change="handshopChange"
            clearable>
            <el-option v-for="(item, index) in workshopOptions" :key="index" :label="item.label" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="piliangPaigong()"
        v-hasPermi="['develop:PaichanTask:piliangBnt']">批量计件派工</el-button>
      <el-button type="info" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="piliangPaigongJishi()"
        v-hasPermi="['develop:PaichanTask:piliangBnt']">批量计时派工</el-button>
      <el-table border v-loading="loading" :data="dataList" ref="table" :max-height="tableMaxHeight" style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" label="序号" type="index" width="50">
          <template slot-scope="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column sortable label="生产单号" align="center" prop="produceNo" />
        <el-table-column label="工程名称" align="center" prop="gongchengName" />
        <el-table-column sortable label="产品编码" align="center" prop="goodsCode" />
        <el-table-column sortable align="center" label="产品名称" prop="goodsName" show-overflow-tooltip />
        <el-table-column sortable align="center" label="产品型号" prop="goodModel" />
        <el-table-column sortable label="工序名称" align="center" prop="name" />
        <el-table-column sortable align="center" label="是否最后一道工序" prop="islast">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.yes_no" :value="scope.row.islast" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="车间" prop="deptName" show-overflow-tooltip />
        <el-table-column align="center" label="标准工时" prop="workHour" show-overflow-tooltip />
        <el-table-column sortable label="生产产品名称" align="center" prop="gongxuName" show-overflow-tooltip />
        <el-table-column sortable label="生产产品型号" align="center" prop="gongxuModel" />
        <el-table-column sortable label="单价" align="center" prop="money" />
        <el-table-column sortable label="总数量" align="center" prop="counts" />
        <el-table-column sortable label="已分配数量" align="center" prop="fenpeiCount" />
        <el-table-column sortable label="已完成数量" align="center" prop="finishCount" />
        <el-table-column sortable label="剩余完成数量" align="center" prop="remainfinishCount" />
        <el-table-column align="center" label="图纸号" prop="shuoming" show-overflow-tooltip />
        <el-table-column align="center" label="产品备注" prop="remark" show-overflow-tooltip />
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button @click="Paigong(scope.row)" icon="el-icon-view" size="mini" type="text"
              v-hasPermi="['develop:PaichanTask:editBnt']">
              计件派工
            </el-button>
            <el-button @click="jishiPaigong(scope.row)" icon="el-icon-view" size="mini" type="text"
              v-hasPermi="['develop:PaichanTask:editBnt']">
              计时派工
            </el-button>
            <el-popconfirm :title="`确认设为最后一道工序当前数据项？`" @confirm="setLast(scope.row)"
                           style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-edit"
                         v-hasPermi="['develop:LnGongyigongxu:remove']">设为最后一道工序</el-button>
            </el-popconfirm>
            <!-- <el-popconfirm :title="`确认要删除该工序吗？`" @confirm="deleteGx(scope.row)"
                         style="margin-left: 10px;" >
            <el-button slot="reference" size="mini" type="text" icon="el-icon-close">
              删除工序
            </el-button>
          </el-popconfirm> -->
          </template>
        </el-table-column>
      </el-table>

      <goods-gongxu-add-form ref="goodsGongxuForm" @ok="getList()"></goods-gongxu-add-form>
      <!--<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>-->
      <goodsSaleDoGongxuForm-all ref="formAll" @ok="getList()"></goodsSaleDoGongxuForm-all>
      <piliangPaigong-form @ok="getList" ref="piliangPaigongForm"></piliangPaigong-form>
      <piliangPaigongJishi-form @ok="getList" ref="piliangPaigongJishiForm"></piliangPaigongJishi-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { deleteAction, getAction } from '@/api/manage'
import GoodsGongxuAddForm from '@/views/develop/goodsSaledoGongxu/module/GoodsGongxuAddForm'
import GoodsSaleDoGongxuFormAll from '@/views/develop/lnGongxu/module/GoodsSaleDoGongxuFormAll'
import PiliangPaigongForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/PiliangPaigongForm";
import PiliangPaigongJishiForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/PiliangPaigongJishiForm";

export default {
  name: "GoodsSaleGoodGongxuForm",
  dicts: ['yes_no'],
  components: {
    GoodsGongxuAddForm,
    GoodsSaleDoGongxuFormAll,
    PiliangPaigongForm,
    PiliangPaigongJishiForm,
  },
  data() {
    return {
      activeName: "first",
      // 弹出层标题
      title: "产品工序查看",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      saleId: null,
      goodsCount: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单产品工艺工序关联表格数据
      dataList: [],
      workshopOptions:[],
      selectRows: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {},
        id: null,
        code: null,
        name: null,
        shuoming: null,
        produceNo: null,
        projectNo: null,
        gongchengName: null,
        goodsName: null,
        goodsCode: null,
        goodModel: null,
        model: null,
        brand: null,
        //isdo: "1",
        //status: "0",
        remark: null,
        projectId: null,
        //deptId: this.$store.getters.deptId,
        deptName: this.$store.getters.deptName,
        orderByColumn: "create_time",
        isAsc: "desc",
        gongxuName: null,
        gongxuModel: null,
      },
      url: {
        list: '/goodsSaledoGongxu/goodsSaledoGongxu/list',
        remove: '/goodsSaledoGongxu/goodsSaledoGongxu/remove/',
        setLast: '/goodsSaledoGongxu/goodsSaledoGongxu/setLast',
        userTreeSelect: '/system/dept/userTreeSelect',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() { 
    this.getUser();
  },
  methods: {
     setLast(row) {
        let params ={"id":row.id,"saleId" : this.queryParams.saleId}
        getAction(this.url.setLast, params).then(response => {
          this.$modal.msgSuccess("设定成功");
          this.getList();
        }).catch(() => {});
      },
    jishiPaigong(row) {
       this.$refs.piliangPaigongForm.piliangFenpeiGxpShow([row]);
    },
    Paigong(row) {
        this.$refs.piliangPaigongForm.piliangFenpeiGxpShow([row]);
    },
    deleteGx(row){
        const ids = row.id ;
        deleteAction(this.url.deleteGx,ids ).then(response => {
          this.getList();
          this.$modal.msgSuccess("删除成功！");
        }).catch(() => {
        });
      },
    getUser() {
      let that = this;
      getAction(that.url.userTreeSelect, { parentId: '57d779853f0f4d14b03d51db30099456' }).then(res => {
        that.workshopOptions = res.data;
      })
    },
    handshopChange(e) {
      // 查找匹配的选项，不仅匹配value，也匹配id
      const selected = this.workshopOptions.find(item => item.value === e || item.id === e);
      if (selected) {
        // 设置表单的部门ID和名称
        // this.queryParams.deptId = selected.id;
        this.queryParams.deptName = selected.label;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    piliangPaigong() {
      this.$refs.piliangPaigongForm.piliangFenpeiGxpShow(this.selectRows);
    },
    piliangPaigongJishi() {
      this.$refs.piliangPaigongJishiForm.piliangFenpeiGxpShow(this.selectRows);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.selectRows = selection;
    },
    setLast(row) {
      let params = { "id": row.id, "saleId": this.queryParams.saleId }
      getAction(this.url.setLast, params).then(response => {
        this.$modal.msgSuccess("设定成功");
        this.getList();
      }).catch(() => { });
    },
    handleUpdateAll() {
      const ids = this.ids;
      this.$refs.formAll.handleUpdate(ids);
    },
    handleAddGoodsGx() {
      this.$refs.goodsGongxuForm.handleAddGoodsGongxu(this.saleId, this.goodsCount);
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    getList() {
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.id)
    //   this.single = selection.length !== 1
    //   this.multiple = !selection.length
    // },
    /** 查询订单产品工艺工序关联列表 */
    xunjianShow(row, show) {
      if (show) {
        this.loading = true;
        this.open = true;
        this.queryParams.saleId = row.goodsSaleId;
        this.getList();
      } else {
        this.loading = true;
        this.open = true;
        this.queryParams.saleId = row.id;
        this.saleId = row.id;
        this.goodsCount = row.count;
        this.getList();
      }

    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    
  }
};
</script>
